package com.bian.plugin.boot.service.impl;

import com.bian.plugin.boot.framework.PluginBeanFactoryClassLoader;
import com.bian.plugin.boot.framework.plugin.PluginInfo;
import com.bian.plugin.boot.framework.support.PluginPathResolver;
import com.bian.plugin.boot.framework.support.PluginRegistery;
import com.bian.plugin.boot.service.PluginService;
import com.bian.plugin.boot.utils.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Service;

/**
 * @author bian
 * @since 1.0
 * 2021/2/22
 */
@Service
public class PluginServiceImpl implements PluginService, InitializingBean {
    private final static Logger LOGGER = LoggerFactory.getLogger(PluginServiceImpl.class);

    @Autowired
    PluginRegistery pluginRegistery;

    @Autowired
    PluginPathResolver pluginPathResolver;

    private boolean initFlag = false;

    @Override
    public boolean load(String jarPath) {
        boolean resultFlag = false;
        PluginInfo pluginInfo = null;
        try {
            // 插件信息解析
            pluginInfo = pluginPathResolver.resolve(jarPath);
            // 将插件注入到注册中心
            boolean registFlag = pluginRegistery.add(pluginInfo);
            if (false == registFlag){
                // 注册失败
                LOGGER.error("路径jarPath={}对应的pluginInfo={} load失败",jarPath,pluginInfo);
                return false;
            }

            LOGGER.info("插件pluginInfo={}加载成功", pluginInfo);
            resultFlag = true;
            return registFlag;
        } catch (Exception ex) {
            LOGGER.error("load jarPath={}失败ex={}", jarPath,ex);
            return false;
        }finally {
//            firePluginStatusChange(pluginInfo,null,"插件加载"+(resultFlag?"成功":"失败"),null);
        }
    }

    @Override
    public boolean unload(String pluginName) {
        return false;
    }

    @Override
    public boolean register(String pluginName) {
        return false;
    }

    @Override
    public boolean unregister(String pluginName) {
        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
//        synchronized (this) {
//            if (false == initFlag) {
//                GenericApplicationContext genericApplicationContext = SpringContextUtils.getGenericApplicationContext();
//                ClassLoader originBeanFactoryClassLoader =
//                        genericApplicationContext.getDefaultListableBeanFactory().getBeanClassLoader();
//                PluginBeanFactoryClassLoader pluginBeanFactoryClassLoader =
//                        new PluginBeanFactoryClassLoader(originBeanFactoryClassLoader, this.pluginRegistery);
//                genericApplicationContext.getDefaultListableBeanFactory()
//                        .setBeanClassLoader(pluginBeanFactoryClassLoader);
//                initFlag = true;
//            }
//        }
    }
}
